<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 基本数据库操作
 * @version 1.0.0 
 * @author Jason.z
 * @copyright AnAn-Studio.Inc
 */
class User_model extends CI_Model {

    const USERS_TBL = "users";
    const GROUPS_TBL = "groups";
    const MODULES_TBL = "modules";
    const MODULE_ACTIONS_TBL = "module_actions";
    const GROUP_PERMISSIONS_TBL = "group_permissions";

  	public function __construct() {
            parent::__construct();
    }

    /**
     * 根据用户ID取出用户
     */
    public function get_user_by_id($user_id)
    {
        if(!empty($user_id)) {
            $this->db->where('id',$user_id);
            $query = $this->db->get(self::USERS_TBL);
            return $query->row_array();
        } else {
            return NULL;
        }
    }

    /**
     * 取出所有用户
     */
    public function get_all_users($group_id = 0)
    {
      $this->db->select(self::USERS_TBL.'.*,'.self::GROUPS_TBL.'.name');
      $this->db->join(self::GROUPS_TBL,self::GROUPS_TBL.'.id='.self::USERS_TBL.'.group');
      if(!empty($group_id)) {
        $this->db->where(self::USERS_TBL.'.group',$group_id);
      }
      $query = $this->db->get(self::USERS_TBL);
      return $query->result_array();
    }

    /**
     * 删除一个用户
     */
    public function delete_one_user($user_id)
    {
      if(!empty($user_id)) {
        $this->db->where('id',$user_id);
        $this->db->delete(self::USERS_TBL);
        return $this->db->affected_rows()>0?TRUE:FALSE;
      }
      return FALSE;
    }

    /**
     * 获取所有组
     */
    public function get_all_groups()
    {
      $query = $this->db->get(self::GROUPS_TBL);
      return $query->result_array();
    }

    /**
     * 更新某个组
     */
    public function update_one_group($data,$where)
    {
      $this->db->update(self::GROUPS_TBL,$data,$where);
      return $this->db->affected_rows()>0?TRUE:FALSE;
    }

    /**
     * 删除某个组
     */
    public function delete_one_group($group_id)
    {
       if(!empty($group_id))
       {
          $this->db->delete(self::GROUPS_TBL,array('id'=>$group_id));
          $this->db->delete(self::GROUP_PERMISSIONS_TBL,array('group_id'=>$group_id));
          return $this->db->affected_rows()>0?TRUE:FALSE;
       }
       return FALSE;
    }

    /**
     * 插入单个模块
     */
    public function insert_one_module($data)
    {
      $this->db->insert(self::MODULES_TBL,$data);
      return $this->db->insert_id();
    }

    /**
     * 更新某个模块
     */
    public function update_one_module($data,$where)
    {
      $this->db->update(self::MODULES_TBL,$data,$where);
      return $this->db->affected_rows()>0?TRUE:FALSE;
    }

    /**
     * 获取某个操作
     */
    public function get_one_action($where=NULL)
    {
      if(!empty($where)) $this->db->where($where);

      $query = $this->db->get(self::MODULE_ACTIONS_TBL);

      return $query->row_array();
    }

    /**
     * 插入单个操作
     */
    public function insert_one_action($data)
    {
      if(is_array($data)&&count($data)>0) {
        $this->db->insert(self::MODULE_ACTIONS_TBL,$data);
        return $this->db->insert_id();
      } else {
        return NULL;
      }
     
    }

    /**
     * 更新某个操作
     */
    public function update_one_action($data,$where)
    {
       if(is_array($data)&&count($data)>0&&is_array($where)) {
        $this->db->update(self::MODULE_ACTIONS_TBL,$data,$where);
        return $this->db->affected_rows()>0?TRUE:FALSE;
       } else {
        return NULL;
       }
     
    }


    /**
     * 获取单个模块
     */
    public function get_one_module($where=NULL)
    {
      if(!empty($where)) $this->db->where($where);

      $query = $this->db->get(self::MODULES_TBL);

      return $query->row_array();
    }

    /**
     * 获取所有的模块
     */
    public function get_all_modules($parent_id=0)
    {
      $this->db->where('parent_id',$parent_id);
      $query = $this->db->get(self::MODULES_TBL);
      return $query->result_array();
    }

    /**
     * 获取某个模块下的操作
     */
    public function get_module_actions($module_id)
    {   
        if(!empty($module_id)) {
          $this->db->where('module_id',$module_id);
          $query = $this->db->get(self::MODULE_ACTIONS_TBL);
          return $query->result_array();
        } else {
          return NULL;
        }    
    }

    /**
     * 取出角色下的模块
     */
    public function get_group_modules($group_id,$parent_id=NUll)
    {
       $this->db->join(self::MODULES_TBL,self::MODULES_TBL.'.module_id=.'.self::GROUP_PERMISSIONS_TBL.'.permission_id');
       $this->db->where(self::GROUP_PERMISSIONS_TBL.'.group_id',$group_id);
       $this->db->where(self::GROUP_PERMISSIONS_TBL.'.type','module');
       if($parent_id !== NULL)
       {
           $this->db->where(self::MODULES_TBL.'.parent_id',$parent_id);
       }  
       $query = $this->db->get(self::GROUP_PERMISSIONS_TBL);
       //echo $this->db->last_query();
       return $query->result_array();
    }

    /**
     * 取出角色下的操作
     */
    public function get_group_actions($group_id)
    {   
       if(!empty($group_id)) {
          $this->db->join(self::MODULE_ACTIONS_TBL,self::MODULE_ACTIONS_TBL.'.action_id='.self::GROUP_PERMISSIONS_TBL.'.permission_id');
          $this->db->where(self::GROUP_PERMISSIONS_TBL.'.group_id',$group_id);
          $this->db->where(self::GROUP_PERMISSIONS_TBL.'.type','action');
          $query = $this->db->get(self::GROUP_PERMISSIONS_TBL);
          //echo $this->db->last_query();
          return $query->result_array();
       } else {
          return NULL;
       }  
    }

}


?>